package com.saishi.servlet;

import com.saishi.dao.UserDao;
import com.saishi.dao.impl.UserDaoImpl;
import com.saishi.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/denglu/*")
public class LoginServlet extends BaseServlet {
    UserDao userDao = new UserDaoImpl();

    public void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //JSP接收过来的账号
        String account = request.getParameter("user-name");
        //JSP接收过来的密码
        String pass = request.getParameter("password");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        //可以用 [用户昵称(唯一属性的)、用户手机号、用户邮箱登录] 第一个参数是昵称  第三个是手机号  第四个是邮箱
        List list = userDao.Login(account, pass, account, account);
        if (list.size() > 0) {
            //输出数据
            System.out.println(list);
            System.out.println("用户:" + account + "  登录成功");
            //把得到的用户信息集合放入sessino 这样放可以用response跳转而且还能出取到值
            request.getSession().setAttribute("userinfo", list);
            response.sendRedirect(request.getContextPath() + "/indexuser.jsp");
        } else {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            System.out.println("信息有误");
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().removeAttribute("userinfo");
        response.sendRedirect("/index.jsp");
        System.out.println("退出登录!");
    }
}
